package com.cola.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cola.dao.CourierDao;
import com.cola.dao.MepDao;
import com.cola.entity.JoinSaleActivity;
import com.cola.entity.courier.Courier;
import com.cola.entity.courier.SignGoods;
import com.cola.entity.order.Order;
import com.cola.entity.order.OrderDetail;
import com.cola.entity.shop.SaleActivity;
import com.cola.enums.OrderDetailType;
import com.cola.enums.SaleType;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Max
 * @since 2017/9/18
 */
public class CourierService {
    private static volatile CourierService instance = null;
    public static CourierService getInstance() {
        if (instance == null) {
            synchronized (CourierService.class) {
                if (instance == null) {
                    instance = new CourierService();
                }
            }
        }
        return instance;
    }
    /**
     * 保存配送员的基本信息
     * @param courier_name 配送员名
     * @param mep_no 经销商Id
     * @param courier_mobile 配送员id
     * @author sunny
     * @since 2017/9/14 14:25
     */
    public Long saveCourierInfo(String courier_name,String mep_no,String courier_mobile) {
        return CourierDao.getInstance().saveCourierInfo(courier_name,mep_no,courier_mobile);
    }

    /**
     * 更新配送员信息
     * @return boolean:true为成功；false为失败
     * @author sunny
     * @since 2017/9/14 15:00
     */
    public Boolean updateCourierInfo(Integer courier_id,String courier_wechat_nick,String courier_open_id,String courier_head_img_url) {
        return CourierDao.getInstance().updateCourierInfo(courier_id,courier_wechat_nick,courier_open_id,courier_head_img_url);
    }

    /**
     * 更新配送员信息
     * @return boolean:true为成功；false为失败
     * @author sunny
     * @since 2017/9/14 15:00
     */
    public Boolean updateCourierInfo(Integer courier_id,String mobile,String courier_name) {
        return CourierDao.getInstance().updateCourierInfo(courier_id,mobile,courier_name);
    }


    /**
     * 删除配送员
     * @return boolean:true为成功；false为失败
     * @author sunny
     * @since 2017/9/14 15:00
     */
    public Boolean deleteCourierInfo(Integer courier_id) {
        return CourierDao.getInstance().deteleCourier(courier_id);
    }

    /**
     * 获取配送员列表
     * @param mepNo 经销商编号
     * @return List<Courier>
     * @author Max
     * @since 2017/9/18
     */
    public List<Courier> getCourierList(String mepNo){
        return CourierDao.getInstance().getCourierList(mepNo);
    }



    /**
     * 获取配送员信息
     * @param courierId 配送员id
     * @return Courier
     * @author Max
     * @since 2017/9/18
     */
    public Courier getCourierInfoById(Integer courierId){
        return CourierDao.getInstance().getCourierInfoById(courierId);
    }


    /**
     * 检查配送员手机号是否存在
     * @param mobile 手机号
     * @return true 可以添加；false不可添加
     * @author Max
     * @since 2017/9/19
     */
    public boolean checkCourierMobile(String mobile){
        return CourierDao.getInstance().checkCourierMobile(mobile);
    }


    /**
     * 获取订单列表
     * @param courierId 配送员id
     * @return List<Order>
     * @author Max
     * @since 2017/9/17
     */
    public List<Order> getCourierAllOrderList(Integer courierId){

        List<Order> orderList = CourierDao.getInstance().getCourierOrderList(courierId);

        List<OrderDetail> orderDetails = CourierDao.getInstance().getCourierOrderDetailList(courierId);

        List<OrderDetail> tempList = new ArrayList<OrderDetail>();
        for(Order order : orderList){
            tempList = new ArrayList<OrderDetail>();
            for (int i = 0; i<orderDetails.size();i++){
                if(orderDetails.get(i).getOrder_no().equals(order.getOrder_no())){
                    tempList.add(orderDetails.get(i));
                    order.setOrder_details(tempList);
                }
            }
        }
        return orderList;
    }


    /**
     * 根据订单编号获取促销信息
     * @param orderNo 订单编号
     * @return JoinSaleActivity
     * @author Max
     * @since 2017/9/20
     */
    public JoinSaleActivity getJoinSaleActivityByOrderNo(String orderNo){
        return CourierDao.getInstance().getJoinSaleActivityByOrderNo(orderNo);
    }



    //===================================签收===========================================

    /**
     * 订单签收
     * @param order 原始订单
     * @param list 签收商品列表
     * @return true;false
     * @author Max
     * @since 2017/9/20
     */
    public boolean doSignOrder(Order order, List<SignGoods> list){
        //获取参与活动的活动配置
        JoinSaleActivity activity = null;

        Integer isSaleStatus =0;

        BigDecimal final_price = new BigDecimal(0);

        Integer goodsNumFinal=0;

        //判断当前签收的数量是否任然满足促销活动
        if(order.getSale_no()!=null){
            activity = CourierDao.getInstance().getJoinSaleActivityByOrderNo(order.getOrder_no());

            List<SaleActivity> listSaleActivity = JSONObject.parseObject(activity.getSale_json(),new TypeReference<List<SaleActivity>>(){});

            Integer saleGoodsNum = 0;
            //获取符合活动的商品总数
            for(SignGoods signGoods : list){
                if(signGoods.getGoods_num_final()>0){
                    goodsNumFinal++;
                }
                for(SaleActivity saleActivity :listSaleActivity){
                    if(signGoods.getGoods_no().equals(saleActivity.getGoods_no())){
                        saleGoodsNum = saleGoodsNum + signGoods.getGoods_num_final();
                    }
                }
            }

            if(saleGoodsNum<listSaleActivity.get(0).getBuy_num()){
                //满减活动
                if(listSaleActivity.get(0).getSale_type().equals(SaleType.PRICE_OFF.getValue())){
                    order.setIs_price_off(0);

                    order.setOff_price_final(new BigDecimal(0));

                    order.setOrder_price_final(order.getOrder_price().add(order.getOff_price()));
                }

                if(listSaleActivity.get(0).getSale_type().equals(SaleType.SEND_GIVEAWAY.getValue())){
                    isSaleStatus=1;
                }
            }else{
                if(listSaleActivity.get(0).getSale_type().equals(SaleType.SEND_GIVEAWAY.getValue())){
                    goodsNumFinal++;
                }
            }
        }

        //详情里面最终价格
        for(OrderDetail detail : order.getOrder_details()){

            if(detail.getType().equals(OrderDetailType.MAIN.getValue())){

                final_price = final_price.add(detail.getGoods_unit_price().multiply(new BigDecimal(detail.getGoods_num_final())));

            }else if(detail.getType().equals(OrderDetailType.GIVEAWAY.getValue())){
                if(isSaleStatus==1){
                    detail.setGoods_num_final(0);
                }
            }

            for(SignGoods signGoods : list){
                if(signGoods.getGoods_no().equals(detail.getGoods_no())){
                    detail.setGoods_num_final(signGoods.getGoods_num_final());
                    detail.setGoods_price_final(detail.getGoods_unit_price().multiply(new BigDecimal(signGoods.getGoods_num_final())));
                }
            }
        }

        //设置最终签收商品件数
        order.setGoods_num_final(goodsNumFinal);
        order.setOrder_price_final(final_price);
        return CourierDao.getInstance().updateFinalOrder(order);
    }





}
